package com.lfy.fyadmin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lfy.fyadmin.common.R;
import com.lfy.fyadmin.entity.UserGroup;
import com.lfy.fyadmin.service.UserGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user-group")
@Tag(name = "用户组关联管理", description = "用户与用户组关联关系接口")
public class UserGroupController {

    private final UserGroupService userGroupService;

    public UserGroupController(UserGroupService userGroupService) {
        this.userGroupService = userGroupService;
    }

    @PostMapping
    @Operation(summary = "新增用户组关联")
    public R<Boolean> save(@RequestBody UserGroup userGroup) {
        boolean result = userGroupService.save(userGroup);
        return result ? R.ok(true) : R.fail("新增失败");
    }

    @PutMapping
    @Operation(summary = "修改用户组关联")
    public R<Boolean> update(@RequestBody UserGroup userGroup) {
        boolean result = userGroupService.updateSelective(userGroup);
        return result ? R.ok(true) : R.fail("修改失败");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户组关联")
    public R<Boolean> delete(@PathVariable Integer id) {
        boolean result = userGroupService.removeById(id);
        return result ? R.ok(true) : R.fail("删除失败");
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询用户组关联")
    public R<UserGroup> getById(@PathVariable Integer id) {
        UserGroup userGroup = userGroupService.getById(id);
        return userGroup != null ? R.ok(userGroup) : R.fail("未找到关联");
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询用户组关联")
    public R<IPage<UserGroup>> findPage(@RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize) {
        Page<UserGroup> page = new Page<>(pageNum, pageSize);
        return R.ok(userGroupService.page(page));
    }
}